﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using InternalManagementSystem.DAO.Models;

/// <summary>
/// 从违规列表进来
/// </summary>
public partial class Violation_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnSave.Visible = false;
            if (Session["Permission"] != null)
            {
                Permission permission = Session["Permission"] as Permission;
                if (permission.IsViolationNoticeEdit)//违章
                {
                    btnSave.Visible = true;
                    
                }

                if (Request.Params["vid"] != null)
                {
                    try
                    {
                        int vid = Convert.ToInt32(Request.Params["vid"]);
                        LoadViolation(vid);

                    }
                    catch
                    {
                    }
                }
            }
        }
    }


    void LoadViolation(int vid)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            ViolationNoticeBLL vbll = new ViolationNoticeBLL(permission);
            DataSet ds = vbll.GetViolationNoticeView(vid);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                lblCompanyName.Text = SwiftConverter.CovertDBObjectToString(dr["CompanyName"]);
                lblUserName.Text = SwiftConverter.CovertDBObjectToString(dr["UserName"]);
                lblSafe.Text = SwiftConverter.CovertDBObjectToYesOrNo(dr["IsSafetyEducation"]);
                txtAddress.Text = SwiftConverter.CovertDBObjectToString(dr["Address"]);
                txtDetails.Text = SwiftConverter.CovertDBObjectToString(dr["Details"]);
                txtNoticeNo.Text= SwiftConverter.CovertDBObjectToString(dr["NoticeNo"]);
                txtViolationDate.Text= SwiftConverter.CovertDBObjectToString(dr["ViolationDate"]);
               
            }
        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["Permission"] != null && Request.Params["vid"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            int vid = Convert.ToInt32(Request.Params["vid"]);
            ViolationNoticeBLL vbll = new ViolationNoticeBLL(permission);

            ViolationNotice pViolationNotice = vbll.GetViolationNotice(vid);
            pViolationNotice.Address = txtAddress.Text;
            pViolationNotice.Details = txtDetails.Text;
            pViolationNotice.NoticeNo = txtNoticeNo.Text;
            pViolationNotice.ViolationDate = SwiftConverter.CovertDBObjectToDataTime(txtViolationDate.Text);

            pViolationNotice.CommitDate = DateTime.Now;

            vbll.UpdateViolationNotice(pViolationNotice);

            Response.Redirect("Index.aspx");

        }
    }


    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("Index.aspx");
    }
}
